import axios from 'axios'
import { MessageBox, Message } from 'element-ui'

const service = axios.create({
    baseURL: process.env.API_ROOT, // url = base url + request url
    timeout: 50000 // request timeout
  })

  service.interceptors.request.use(
    config => {
    //   if (store.getters.token) {
    //     config.headers.Authorization = getToken()
    //     config.headers.Accept = 'application/json'
    //     config.headers.VC = vc(config.data)
    //     config.headers.marketId = store.state.app.marketId
    //   }
      return config
    },
    error => {
    //   console.log(error)
      return Promise.reject(error)
    }
  )

  service.interceptors.response.use(
    response => {
        const res = response.data
        if (res.code !== 200) {
            return Promise.reject(new Error(res.message || 'Error'))
        }else{
            return response
        }
    },error=>{
      let mes=''
      if(error.response.data.error){
        mes=error.response.data.error
      }
      else mes=error.response.data.message
      // console.log(error.response);
        Message({
            message: mes,
            type: 'error',
          })
        return Promise.reject(error)
    })

    export default service